import request from '@/utils/request'
import Qs from 'qs'

export function getSpace(size, page, kw) {
    return request({
        url: '/space/?size=' + size + '&page=' + page + ((kw) && kw != '' ? '&kw=' + kw : ''),
        method: 'get'
    })
}

export function addSpace(name, user_id, user_name) {
    return request({
        url: '/space/',
        method: 'post',
        headers: { 'Content-Type': 'application/x-www-form-urlencoded, multipart/form-data' },
        transformRequest: [function(data) {
            data = Qs.stringify(data)
            return data
        }],
        data: {
            name: name,
            user_id: user_id,
            user_name: user_name
        }
    })
}

export function updateSpace(id, name, user_id, user_name) {
    return request({
        url: '/space/' + id,
        method: 'put',
        headers: { 'Content-Type': 'application/x-www-form-urlencoded, multipart/form-data' },
        transformRequest: [function(data) {
            data = Qs.stringify(data)
            return data
        }],
        data: {
            name: name,
            user_id: user_id,
            user_name: user_name
        }
    })
}

export function deleteSpace(id, name, user_id, user_name) {
    return request({
        url: '/space/' + id,
        method: 'delete',
        headers: { 'Content-Type': 'application/json' },
        transformRequest: [function(data) {
            data = Qs.stringify(data)
            return data
        }],
        data: {
            name: name,
            user_id: user_id
        }
    })
}

export function getSpaceInfo(id) {
    return request({
        url: '/space/' + id,
        method: 'get'
    })
}

export function getSpaceMembers(id, size, page, kw) {
    return request({
        url: '/space/' + id + "/members?size=" + size + ((page && page !== "") ? "&page=" + page : "") + ((kw && kw !== "") ? ("&kw=" + kw) : ""),
        method: 'get'
    })
}

export function editSpaceMember(id, member) {
    return request({
        url: '/space/' + id + '/member',
        method: 'put',
        headers: { 'Content-Type': 'application/x-www-form-urlencoded, multipart/form-data' },
        transformRequest: [function(data) {
            data = Qs.stringify(data)
            return data
        }],
        data: member
    })
}

export function editSpace(id, data) {
    return request({
        url: '/space/' + id,
        method: 'put',
        headers: { 'Content-Type': 'application/x-www-form-urlencoded, multipart/form-data' },
        transformRequest: [function(data) {
            data = Qs.stringify(data)
            return data
        }],
        data: data
    })
}

export function addSpaceMember(space_id, user_id, role) {
    return request({
        url: '/space/' + space_id + '/add_member?user_id=' + user_id + "&role=" + role,
        method: 'put',
        headers: { 'Content-Type': 'application/x-www-form-urlencoded, multipart/form-data' },
        transformRequest: [function(data) {
            data = Qs.stringify(data)
            return data
        }],
        data: {}
    })
}

export function removeSpaceMember(space_id, user_id) {
    return request({
        url: '/space/' + space_id + '/remove_member?user_id=' + user_id,
        method: 'put',
        headers: { 'Content-Type': 'application/x-www-form-urlencoded, multipart/form-data' },
        transformRequest: [function(data) {
            data = Qs.stringify(data)
            return data
        }],
        data: {}
    })
}